package com.qfedu.dao.impl;


import com.qfedu.dao.AdministratorDao;
import com.qfedu.dao.CartDao;
import com.qfedu.model.Cart;
import com.qfedu.utils.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 王海龙
 * @date 2023-04-18
 */
public class CartDaoImpl implements CartDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());
    private QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());

    @Override
    public int bindUser(int id) {
        String sql ="insert into cart values(null,?)";
        int update = jdbcTemplate.update(sql, id);
        return update;
    }

    @Override
    public Cart findCartByUid(int id) {
        String sql = "select * from cart where id=?";
        try {
            Cart cart = queryRunner.query(sql, new BeanHandler<>(Cart.class),id);
            return cart;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
